from bag import Bag

def breaks(nums0,bins=5) :
   def chunks():
    start  = stop = 0 
    while stop  < n: 
      start = stop  
      stop = start + d 
      while stop < n-1 and nums[stop-1] == nums[stop]: 
        stop += 1
      if stop + d >= n : 
        stop = n 
      yield nums[start:stop]
   nums = sorted([x for x in nums0 if x != "?"])
   n    = len(nums)
   d    = max(1,int(n/bins))
   tmp  = [Bag(lst=chunk) for chunk in chunks()]
   return rc(tmp, minObs = d)

def bin(val,breaks, ninf = -10*10) :
  if val == "?": return val
    before, last = ninf, 0
    for i,now in enumerate(breaks) :
      if val > before and val <= now: return i
        before, last  = now, i
  return last+1
